package com.atguigu.srb.core.controller.admin;

import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.pojo.vo.BorrowerApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerDetailVO;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.result.R;
import com.atguigu.srb.core.service.BorrowerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/core/borrower")
@Api(tags = "后台借款人管理")
@Slf4j
public class AdminBorrowerController {

    @Autowired
    private BorrowerService borrowerService;

    /**
     * 额度审批的目标：
     * (1)在user_integral表中添加积分明细
     * (2)在user_info表中添加总积分（user_info表中的原始积分 + user_integral表中的积分明细之和 ）
     * (3)修改borrower表的借款申请审核状态
     * (4)修改user_info表中的借款申请审核状态
     * @param borrowerApprovalVO
     * @return
     */
    @ApiOperation(value="借款额度审核")
    @PostMapping("/approval")
    public R getApproval(@RequestBody BorrowerApprovalVO borrowerApprovalVO){
        borrowerService.getApproval(borrowerApprovalVO);
        return R.ok();
    }

    @ApiOperation(value = "借款人详情显示")
    @GetMapping("/show/{borrowerId}")
    public R getDetail(@PathVariable("borrowerId")@ApiParam(value = "借款人id",required = true,example = "1") Long borrowerId){

        BorrowerDetailVO borrowerDetailVO = borrowerService.getDetailByBorrowerId(borrowerId);
        if (ObjectUtils.isEmpty(borrowerDetailVO)){
            return R.error();
        }
        else  return R.ok().data("borrowerDetailVO",borrowerDetailVO);
    }


    @ApiOperation(value = "获取借款人分页列表")
    @GetMapping("/listPage/{currentPage}/{limit}")
    public R listPage(@PathVariable("currentPage") Long currentPage,
                      @PathVariable("limit") Long limit,
                      @RequestParam(required = false) String keyWord) {
        // 创建分页对象
        Page<Borrower> borrowerPage = new Page<>(currentPage,limit);
        // 判断keyword是否为空,没有传查询全部
        if (StringUtils.isEmpty(keyWord)){
            QueryWrapper<Borrower> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("id");
            borrowerPage = borrowerService.page(borrowerPage, queryWrapper);
            log.info("----借款人列表--->>>"+borrowerPage);
            return R.ok().data("borrowerPageInfo",borrowerPage);
        }
        // 传入keyword有值,封装查询条件
        QueryWrapper<Borrower> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",keyWord)
                .or().like("mobile",keyWord)
                .or().like("id_card",keyWord)
                .orderByDesc("id");
        borrowerPage = borrowerService.page(borrowerPage,queryWrapper);
        log.info("----借款人列表--->>>"+borrowerPage.getRecords());
        if (ObjectUtils.isEmpty(borrowerPage)){
            return R.error().message("借款人信息分页查询失败!");
        }

        // 返回数据
        return R.ok().data("borrowerPageInfo",borrowerPage);
    }


}
